'''
Created on 01/12/2009

@author: eh01
'''
from ConfigParser import RawConfigParser
from Queue import Queue
from datastore import database
from datastore.database import DataStore
from datastore.resource import Resource
from runtime import rt
from runtime.spaces import SpacesContext, Spaces
from spear.a2ptask import A2PTask
import logging
import os
import time
import unittest

LOGGING_LEVEL = logging.DEBUG

class A2PTaskTest(unittest.TestCase):


    def setUp(self):
        queue = Queue()
        self.spaces = Spaces()
        spaces_context = SpacesContext(self.spaces, queue)
        runtime = rt.Runtime()
        runtime.initialise_spaces_with_local_config_files(spaces_context)       

        self.temp_database_name = 'test.db'
        self.original_database_name = database.DATABASE_NAME
        database.DATABASE_NAME = self.temp_database_name
        
        section = 'a2p data'
        self.cp = RawConfigParser()
        # the code coverage test does some magic when executing. And the properties_file_path has to be change to use the one below
        # which only just specifies the file name
#        properties_file_path = 'a2p.properties'
        properties_file_path = os.path.dirname(__file__) + '/a2p.properties'
        self.cp.read(properties_file_path)
        self.council_username = self.cp.get(section, 'council.username')
        self.council_password = self.cp.get(section, 'council.password')
        self.attachments_path = self.cp.get(section, 'attachmentspath')

        
    def tearDown(self):
        try:
            os.remove(self.temp_database_name)
        except:
            pass   

    def test_cb_generate_a2p_request_input(self):
        logger = logging.getLogger()
        logger.setLevel(LOGGING_LEVEL)
        rs_1 = Resource(namespaces=self.spaces.ns)
        rs_2 = Resource(namespaces=self.spaces.ns)
        rs_3 = Resource(namespaces=self.spaces.ns)
        rs_4 = Resource(namespaces=self.spaces.ns)
        
        rs_1.load_from_file(os.path.dirname(__file__) + '/test-op-app-list.xml')
        rs_2.load_from_file(os.path.dirname(__file__) + '/test-instr-login-get.xml')
        rs_3.load_from_file(os.path.dirname(__file__) + '/test-instr-login-post.xml')
        rs_4.load_from_file(os.path.dirname(__file__) + '/test-instr-app-list-search-get.xml')
        
        ds = DataStore()
        ds.put([rs_1, rs_2, rs_3, rs_4])
                
        queue = Queue()
        spaces_context = SpacesContext(self.spaces, queue)
        uri_a2p = spaces_context.uri('a2p:')        
        uri_tasks = spaces_context.uri('tasks:')
        # dummy task def resource
        task_def_resource = Resource()
        a2p_task = A2PTask(self.spaces, uri_tasks.eplanning.spear.a2ptask, task_def_resource)
        a2p_task.log_level(LOGGING_LEVEL)
        a2p_task.start()
        time.sleep(1)
        res = Resource()
        res.params = {'j_username':self.council_username, 'j_password':self.council_password}
        new_uri = spaces_context.put(uri_a2p.operation.applisterrormsg, res)
        rs = spaces_context.get_wait(new_uri.response)
        spaces_context.delete(uri_tasks.eplanning.spear.a2ptask)
        self.assertEqual(rs.error, 'You must specify a keyword.')
        
if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()